package com.masterclient.app.modules.goods.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.commonbean.product.CommodityOversea;
import com.eoner.baselibrary.bean.goods.CommodityService;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.bean.goods.ProductPromotionMessage;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;

import java.util.List;

public class CommodityDetailServiceAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public interface OnServiceCallBack {
        void couponClick();

        void serviceClick();
    }

    OnServiceCallBack onServiceCallBack;

    public OnServiceCallBack getOnServiceCallBack() {
        return onServiceCallBack;
    }

    public void setOnServiceCallBack(OnServiceCallBack onServiceCallBack) {
        this.onServiceCallBack = onServiceCallBack;
    }

    public CommodityDetailServiceAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null);
    }

    public CommodityDetailServiceAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View coupon_layout = holder.itemView.findViewById(R.id.coupon_layout);
        View view_coupon = holder.itemView.findViewById(R.id.view_coupon);
        View service_layout = holder.itemView.findViewById(R.id.service_layout);
        View coupon_line_layout = holder.itemView.findViewById(R.id.coupon_line_layout);
        LinearLayout coupon_content_layout = holder.itemView.findViewById(R.id.coupon_content_layout);
        LinearLayout ll_detail_present_container = holder.itemView.findViewById(R.id.ll_detail_present_container);
        TextView gridLayout = holder.itemView.findViewById(R.id.gridLayout);
        View llActivity = holder.itemView.findViewById(R.id.ll_activity);
        TextView tvActivityDes = holder.itemView.findViewById(R.id.tv_activity_des);
        List<CouponMessage> couponMessages = data.getCoupons();
        //优惠券
        if (couponMessages != null && couponMessages.size() > 0) {
            view_coupon.setVisibility(View.VISIBLE);
            coupon_line_layout.setVisibility(View.VISIBLE);
            coupon_content_layout.removeAllViews();
            int num = 0;
            for (CouponMessage coupon : couponMessages) {
                TextView tv_coupon = (TextView) View.inflate(mContext, R.layout.item_coupon_text, null);
                tv_coupon.setText(coupon.getSubName());
                coupon_content_layout.addView(tv_coupon);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv_coupon.getLayoutParams();
                params.leftMargin = ScreenUtils.dp2px(6);
                tv_coupon.setLayoutParams(params);
                if (num == 2) {//只显示两条
                    break;
                }
                num++;
            }
        } else {
            view_coupon.setVisibility(View.GONE);
            coupon_line_layout.setVisibility(View.GONE);
        }
        //满减
        if (data.getMeet_reduce_promotion() != null && !TextUtils.isEmpty(data.getMeet_reduce_promotion().getName())) {
            llActivity.setVisibility(View.VISIBLE);
            tvActivityDes.setText(data.getMeet_reduce_promotion().getName());
        } else {
            llActivity.setVisibility(View.GONE);
        }
        //赠品列表
        List<ProductPromotionMessage> productPromotionMessages = data.getPromotions();
        if (productPromotionMessages != null && productPromotionMessages.size() > 0) {
            ll_detail_present_container.setVisibility(View.VISIBLE);
            ll_detail_present_container.removeAllViews();
            int num = 1;
            for (ProductPromotionMessage promotionMessage : productPromotionMessages) {
                View view_present_goods = View.inflate(mContext, R.layout.item_cart_present, null);
                TextView tvPresentTag = view_present_goods.findViewById(R.id.tv_present_tag);
                TextView tvPresentName = view_present_goods.findViewById(R.id.tv_present_name);
                TextView tvPresentNum = view_present_goods.findViewById(R.id.tv_present_num);

                tvPresentTag.setText(promotionMessage.getTag());
                tvPresentName.setText(promotionMessage.getName());
                tvPresentNum.setVisibility(View.GONE);

                ll_detail_present_container.addView(view_present_goods);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view_present_goods.getLayoutParams();
                if (num == 1) {
                    if (couponMessages != null && couponMessages.size() > 0) {
                        params.topMargin = ScreenUtils.dp2px(13);
                    } else {
                        params.topMargin = ScreenUtils.dp2px(0);
                    }
                } else {
                    params.topMargin = ScreenUtils.dp2px(0);
                }
                if (productPromotionMessages.size() == num) {
                    params.bottomMargin = ScreenUtils.dp2px(0);
                } else {
                    params.bottomMargin = ScreenUtils.dp2px(13);
                }
                view_present_goods.setLayoutParams(params);
                num++;
            }
        } else {
            ll_detail_present_container.setVisibility(View.GONE);
        }

        coupon_layout.setOnClickListener(view -> {
            if (onServiceCallBack != null) {
                onServiceCallBack.couponClick();
            }
        });
        if ((couponMessages == null || couponMessages.size() <= 0) && data.getMeet_reduce_promotion() == null &&
                (productPromotionMessages == null || productPromotionMessages.size() <= 0)){
            coupon_layout.setVisibility(View.GONE);
        } else {
            coupon_layout.setVisibility(View.VISIBLE);
        }

        //服务
        List<CommodityService> services = data.getServices();
        if (services != null) {
            String str = "";
            int num = 0;
            for (CommodityService service : services) {
                num++;
                if (num == services.size()) {
                    str = str + service.getTitle();
                } else {
                    str = str + service.getTitle() + " · ";
                }
            }
            gridLayout.setText(str);
        }

        service_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onServiceCallBack != null) {
                    onServiceCallBack.serviceClick();
                }
            }
        });

        //发货
        CommodityOversea oversea = data.getOversea();
        View send_goods_layout = holder.itemView.findViewById(R.id.send_goods_layout);
        ImageView iv_send_goods = holder.itemView.findViewById(R.id.iv_send_goods);
        if (oversea != null && !TextUtils.isEmpty(oversea.getShipping_image())) {
            send_goods_layout.setVisibility(View.VISIBLE);
            ViewTransformUtil.glideImageView(mContext, oversea.getShipping_image(), iv_send_goods, 0);
        } else {
            send_goods_layout.setVisibility(View.GONE);
        }

    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 3;
    }
}
